package Lab1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Regression{
	
	public static int countLines() throws IOException{
		//Counting number of lines in a File
		File f = new File("F:\\Softwares\\EclipsePortable\\Data\\workspace\\AI\\src\\lab1_input1.txt");
		FileReader fr = new FileReader(f);
		BufferedReader br = new BufferedReader(fr);
		String data;
		int count = 0;
		while( (data = br.readLine()) != null){
			count++;
		}
		br.close();
		return count;
	}
	public static double calculateScope(double sumx,double sumy,double sumxy,double sumxx,int n){
		double numenator = ( (n * sumxy) -  ((sumx)*(sumy)) );
		double denominator = (n*sumxx - (sumx*sumx));
		double slope_b = numenator / denominator;
		return slope_b;	
	}
	public static double calculateIntercept(double sumx,double sumy,double slope_b,int n){
		double intercept_a =  (sumy - (slope_b*(sumx)) ) / n;
		return intercept_a;
	}
	public static void printResult(Element [] elements,double num,double intercept_a,double slope_b){
		int len = elements.length;
		for(int i = 0; i < len; i++){
			if(num == elements[i].getX()){
				System.out.println(elements[i].getY());
				return;
			}
		}
		double result = (intercept_a + (slope_b * num));
		result = Math.round(result*100.0)/100.0;
		System.out.println(result+"");
	}
	public static void main(String[] args) throws IOException {
		int n = countLines();
		File f = new File("F:\\Softwares\\EclipsePortable\\Data\\workspace\\AI\\src\\lab1_input1.txt");
		FileReader fr = new FileReader(f);
		BufferedReader br = new BufferedReader(fr);
		Element [] elements = new Element[n];
		String data;
		int i = 0;
		while( (data = br.readLine()) != null){
			elements[i] = new Element();
			StringTokenizer st = new StringTokenizer(data,"	");
			double x = Double.parseDouble(st.nextToken());
			elements[i].setX(x);
			double y = Double.parseDouble(st.nextToken());
			elements[i].setY(y);
			i++;
		}
		br.close();
		for(int j = 0; j < i;j++){
			elements[j].setXy();
			elements[j].setXx();
		}
		
		//System.out.println(elements[0].getY());
		//Find sum(x)
		double sumx = 0;
		for(int j = 0; j < i;j++){
			sumx  = sumx + elements[j].getX();
		}
		//Find sum(y)
		double sumy = 0;
		for(int j = 0; j < i;j++){
			sumy  = sumy + elements[j].getY();
		}
		//find sum(xy)
		double sumxy = 0;
		for(int j = 0; j < i;j++){
			sumxy  = sumxy + elements[j].getXy();
		}
		//find sum(xx)
		double sumxx = 0;
		for(int j = 0; j < i;j++){
			sumxx  = sumxx + elements[j].getXx();
		}
		double slope_b = calculateScope(sumx, sumy, sumxy, sumxx, n);
		double intercept_a = calculateIntercept(sumx, sumy, slope_b, n);
		
		
		Scanner sc = new Scanner(System.in);
		System.out.println("Enter a number to calculate value of: ");
		double num = sc.nextInt();
		printResult(elements, num, intercept_a, slope_b);
	}
}